package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;
    //查询所有库存列表
    @Override
    public List<Goods> getGoods(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Goods> list=goodsDao.selectGoodsList(page, rows,codeOrName,goodsTypeId);
        return list;
    }

    @Override
    public void addOrUpdate(Goods goods) {
        if (goods!=null){
            goods.setInventoryQuantity(100);//添加窗口没有天库存数胡亮，默认给100
            goods.setState(0);
            String maxCode = goodsDao.getMaxCode();//获取当前最大编码值
            Integer code=Integer.parseInt(maxCode)+1;
            goods.setGoodsCode(code.toString());
            if (goods.getGoodsId()==null){//添加
                goodsDao.add(goods);
            }else {
                goodsDao.update(goods);
            }
        }
    }

    @Override
    public ServiceVO deleteById(Integer goodsId) {
        Goods goods=goodsDao.getGoodsById(goodsId);
        if (goods!=null){
            Integer state = goods.getState();
            //0表示初始值,1表示已入库，2表示有进货或销售单据
            if (state==0){//判断商品状态,有入库、有进货和销售单据的不能删除。
                goodsDao.deleteById(goodsId);
                return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
            }else if (state==1){
                return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
            }else {//state==2
                return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
            }
        }
        return new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
    }

    //无库存商品列表展示（可以根据商品名称或编码查询）
    @Override
    public List<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page=(page-1)*rows;
        List<Goods> noInventoryQuantity = goodsDao.getNoInventoryQuantity(page, rows, nameOrCode);
        for (Goods goods : noInventoryQuantity) {
            Integer goodsTypeId = goods.getGoodsTypeId();
           GoodsType goodsType=goodsTypeDao.getGoodsTypeById(goodsTypeId);
            goods.setGoodsTypeName(goodsType.getGoodsTypeName());
        }
        return noInventoryQuantity;
    }

    //有库存商品列表展示（可以根据商品名称或编码查询）
    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page=(page-1)*rows;
        List<Goods> hasInventoryQuantity = goodsDao.getHasInventoryQuantity(page, rows, nameOrCode);
        for (Goods goods : hasInventoryQuantity) {
            Integer goodsTypeId = goods.getGoodsTypeId();
            GoodsType goodsType=goodsTypeDao.getGoodsTypeById(goodsTypeId);
            goods.setGoodsTypeName(goodsType.getGoodsTypeName());
        }
        return hasInventoryQuantity;
    }

    //添加库存、修改数量或成本价-->都是修改操作
    @Override
    public ServiceVO updateStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Integer row=goodsDao.updateStock(goodsId,inventoryQuantity,purchasingPrice);
        if (row>0){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }else {
            return new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
        }
    }

    //删除库存（要判断商品状态入库、有进货和销售单据的不能删除）
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods=goodsDao.getGoodsById(goodsId);
        if (goods!=null){
            Integer state = goods.getState();
            //0表示初始值,1表示已入库，2表示有进货或销售单据
            if (state==0){//判断商品状态,有入库、有进货和销售单据的不能删除。
                goodsDao.deleteStock(goodsId);
                return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
            }else if (state==1){
                return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
            }else {//state==2
                return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
            }
        }
        return new ServiceVO(ErrorCode.REQ_ERROR_CODE,ErrorCode.REQ_ERROR_MESS);
    }

    //查询所有当前库存量小于库存下限的商品信息
    @Override
    public List<Goods> selectListAlarm() {
        return goodsDao.selectListAlarm();
    }


}
